package client.global.commlib.dialog

import android.content.Context
import android.os.Handler
import android.os.Looper.getMainLooper
import android.view.Gravity
import android.view.WindowManager
import client.global.commlib.R
import client.global.commlib.databinding.LayoutJtoastBinding
import com.tzstore.oo.dialog.CenterDialog

open class JToastDialog(context: Context) :
    CenterDialog<LayoutJtoastBinding>(context, R.style.toastDialog) {


    val handler: Handler by lazy {
        Handler(getMainLooper()) { msg ->
            when (msg.what) {
                0 -> {
                    cancel()
                }
            }
            false
        }
    }

    var msg: String = ""

    override fun getV(): Int {
        return R.layout.layout_jtoast
    }

    override fun show() {
        super.show()
        handler.removeMessages(0)
        handler.sendEmptyMessageDelayed(0, 1000)

    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        handler.removeMessages(0)
    }


    override fun postBinding() {
        binding.event = this
        binding.text.text = msg
        setCanceledOnTouchOutside(true)
        widthMode = 1
        window?.apply {
            setGravity(Gravity.CENTER)
            setLayout(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT
            )
        }
    }
}